package com.oracle.role.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.oracle.mapper.RolesMapper;
import com.oracle.pojo.Permission;
import com.oracle.pojo.Roles;
import com.oracle.role.service.api.RoleServiceApi;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleServiceApi {

    @Autowired
    private RolesMapper rolesMapper;

    @Transactional(readOnly = true,propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<List<Roles>> findRoleAll() {
        List<Roles> rolesList = this.rolesMapper.selectByExample(null);
        return new ResultEntity<List<Roles>>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,rolesList);
    }

    @Transactional(readOnly = true,propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<List<Permission>> getPermissionsAll() {
        List<Permission> perList = this.rolesMapper.selectPermissionsAll();
        return new ResultEntity<List<Permission>>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,perList);
    }

    @Transactional(readOnly = true,propagation = Propagation.REQUIRED)
    @Override
    public ResultEntity<List<Integer>> getPermissionsByRole(Integer id) {
        List<Integer> perids = this.rolesMapper.selectPermissionsByRoleId(id);
        return new ResultEntity<List<Integer>>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,perids);
    }

    @Transactional(readOnly = true,propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<Roles> getRoleById(Integer id) {
        Roles roles = this.rolesMapper.selectByPrimaryKey(id);
        return new ResultEntity<Roles>(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS,roles);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public ResultEntity addRoleVindicatePermissions(Roles roles,Integer...perids) {
        //添加角色信息，获取角色的ID
        this.rolesMapper.insert(roles);
        //维护角色下的权限信息
        for(int i=0 ; i<perids.length ; i++){
            this.rolesMapper.insertRoleAndPermissions(roles.getId(),perids[i]);
        }
        return new ResultEntity(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public ResultEntity updateRoleVindicate(Roles roles, Integer[] perids) {
        //修改角色信息
        this.rolesMapper.updateByPrimaryKeySelective(roles);
        //先删除角色下所有的权限
        this.rolesMapper.deletePermissionsByRoleId(roles.getId());
        //维护角色下的权限信息
        for(int i=0 ; i<perids.length ; i++){
            this.rolesMapper.insertRoleAndPermissions(roles.getId(),perids[i]);
        }
        return new ResultEntity(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public ResultEntity deleteRoleAndPermissions(Integer id) {
        this.rolesMapper.deleteByPrimaryKey(id);
        this.rolesMapper.deletePermissionsByRoleId(id);
        return new ResultEntity(ResultEntity.CODE_SUCCESS,ResultEntity.MSG_SUCCESS);
    }
}
